package com.hhsj.design.pullableview.testScroller;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffXfermode;
import android.graphics.Rect;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.LinearInterpolator;
import android.widget.Scroller;


/**
 * 制作一个回弹视图，可以设置其回弹位置，仿支付宝首页
 */
public class LinearScrollerLayout extends ViewGroup {


    private Scroller mScroller;

    public LinearScrollerLayout(Context context) {
        this(context, null);
    }

    public LinearScrollerLayout(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public LinearScrollerLayout(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }


    private void init() {
        mScroller = new Scroller(getContext(), new LinearInterpolator());
    }


    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
        int count = getChildCount();
        int childTop = t;
        int cl;
        int cr;
        int ct;
        int cb;
        for (int i = 0; i < count; i++) {
            View child = getChildAt(i);
            MarginLayoutParams params = (MarginLayoutParams) child.getLayoutParams();
            cl = l + params.leftMargin;
            ct = childTop + params.topMargin;

            if (getMeasuredWidth() - getPaddingLeft() - getPaddingRight() <= params.leftMargin + child.getMeasuredWidth() + params.rightMargin) {
                cr = r - params.rightMargin;
            } else {
                cr = l + params.leftMargin + child.getMeasuredWidth();
            }

            cb = childTop + params.topMargin + child.getMeasuredHeight();
            child.layout(cl, ct, cr, cb);
            childTop = cb + params.bottomMargin;
        }
    }


    /**
     * 这是一个至上而下的线性布局，高度由自身和child决定
     * 宽度由最宽的决定，排列从左到右，从上到下
     * 测量
     * 布局
     *
     * @param widthMeasureSpec
     * @param heightMeasureSpec
     */
    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {

        int widthSize = MeasureSpec.getSize(widthMeasureSpec);
        int heightSize = MeasureSpec.getSize(heightMeasureSpec);

        int widthMode = MeasureSpec.getMode(widthMeasureSpec);
        int heightMode = MeasureSpec.getMode(heightMeasureSpec);

        int width = 0;
        int height = 0;

        int cCount = getChildCount();
        for (int i = 0; i < cCount; i++) {
            View child = getChildAt(i);
            if (child.getVisibility() == GONE) {
                continue;
            }

            // 测量每一个child的宽和高
            measureChild(child, widthMeasureSpec, heightMeasureSpec);
            // 得到child的lp
            MarginLayoutParams lp = (MarginLayoutParams) child.getLayoutParams();
            // 当前子空间实际占据的宽度
            int childWidth = child.getMeasuredWidth() + lp.leftMargin + lp.rightMargin;
            // 当前子空间实际占据的高度
            int childHeight = child.getMeasuredHeight() + lp.topMargin + lp.bottomMargin;

            //宽度
            width = Math.max(widthSize, childWidth);
            height = height + childHeight;
        }


        width = getPaddingLeft() + width + getPaddingRight();
        height = getPaddingTop() + height + getPaddingBottom();

        setMeasuredDimension((widthMode == MeasureSpec.EXACTLY) ? widthSize : width, (heightMode == MeasureSpec.EXACTLY) ? heightSize : height);

    }

    @Override
    public boolean dispatchTouchEvent(MotionEvent ev) {
        return super.dispatchTouchEvent(ev);
    }

    @Override
    public boolean onInterceptTouchEvent(MotionEvent ev) {
        return super.onInterceptTouchEvent(ev);
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        return super.onTouchEvent(event);
    }


    @Override
    protected LayoutParams generateLayoutParams(LayoutParams p) {
        return new MarginLayoutParams(p);
    }

    @Override
    public LayoutParams generateLayoutParams(AttributeSet attrs) {
        return new MarginLayoutParams(getContext(), attrs);
    }

    @Override
    protected LayoutParams generateDefaultLayoutParams() {
        return new MarginLayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
    }

    @Override
    public void computeScroll() {
        if (mScroller.computeScrollOffset()) {
            scrollTo(mScroller.getCurrX(), mScroller.getCurrY());
            invalidate();
        }
    }


    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
    }

    @Override
    protected void dispatchDraw(Canvas canvas) {
        super.dispatchDraw(canvas);


        Rect rect = new Rect(0, 0, getWidth(), getHeight());
        RectF rectF = new RectF(rect);

        Bitmap bitmap = Bitmap.createBitmap(getWidth(), getHeight(), Bitmap.Config.ARGB_8888);
        bitmap.eraseColor(0xffff3333);
        Canvas bCnv = new Canvas(bitmap);


//        Paint paint = new Paint();
//        paint.setColor(0xFF003333);
//        paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_OUT));
//        bCnv.drawRoundRect(rectF, 30, 30, paint);

        Path mPath = new Path();

        int radius = 100;

        mPath.addRoundRect(new RectF(0, 0, getWidth(), getHeight()),
                new float[]
//                        {
//                                radius, radius,
//                                getWidth() - radius, radius,
//                                getWidth() - radius, getHeight() - radius,
//                                radius, getHeight() - radius
//                        }
//                        ,
                        {
                                radius, radius,
                                radius, radius,
                                radius, radius,
                                radius, radius
                        }

                , Path.Direction.CCW);
        canvas.clipPath(mPath);
        Paint paint = new Paint();
//        paint.setColor(0xFF003333);
        paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_IN));
        canvas.drawBitmap(bitmap, rect, rect, paint);
    }
}
